// pages/cardlist/cardlit.js
var requestutil = require('../../utils/request.js')
var util = require('../../utils/util.js')
import config from '../../utils/config.js'
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {},
    hasUserInfo: false,
    shouerUser:null,
    patientList:{},
    hasPatientList:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu({

    });
    var that=this;
    //获取用户微信基本信息  包括头像 昵称等
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          console.info("getUserInfo------success");
        }
      })
    }

    this.getCardListWithLoginCheck();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCardListWithLoginCheck();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //点击获取微信头像信息后的回调
  bindGetUserInfo: function (e) {
    let that = this;
    app.globalData.userInfo = e.detail.userInfo;
    that.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getCardListWithLoginCheck:function()
  {
    var that = this;
    //请求获取首儿用户信息
    if (app.globalData.shouerUser) {
      //已经登录成功  请求获取课程详细信息
      that.setData({
        shouerUser: app.globalData.shouerUser
      });
      that.getCardList();
    } else {
      //在app.js onLaunch方法里还没有登录成功
      app.shouerlogin(function () {
        //首儿登录成功函数
        console.debug("进入登录成功回调函数");
        that.setData({
          shouerUser: app.globalData.shouerUser
        });
        that.getCardList();
      },
        function () {
          //首儿登录失败函数
          console.debug("进入登录失败回调函数");
          wx.stopPullDownRefresh();
        });
    }
  },
  getCardList:function()
  {
    var that =this;
    var param={};
    param.userId = app.globalData.shouerUser.userId;

    requestutil.sendRequest("queryPatientList",param,function(res){
      console.info(res);
      wx.stopPullDownRefresh();
      if (res.data.errorCode >= 0) { 

        if (res.data.patientList)
        {
          if (res.data.patientList.length>0)
          {
            that.setData({
              hasPatientList: true,              
              patientList: res.data.patientList
            });

          }else
          {
            that.setData({
              hasPatientList: false,
              patientList: res.data.patientList
            });
          }

        }else
        {
          that.setData({
            hasPatientList: false,
            patientList: res.data.patientList
          });
        }
      
      }else
      {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none'
        })
      }

    },function()
    {
      console.error("查询就诊卡信息失败");
      wx.stopPullDownRefresh();
      wx.showToast({
        title: "查询就诊卡信息失败!",
        icon: 'none'
      })
    });
  },
  onCardClick:function(e)
  {
    var index = e.currentTarget.dataset.current;
    if (this.data.patientList && index < this.data.patientList.length) {
      var card = this.data.patientList[index];
      if (card) {
        var param = "card={0}";
        param = String.format(param, JSON.stringify(card));
        wx.navigateTo({
          url: '/pages/card/card?' + param,
        })
      }
    }
  },
  onCardClick:function(e)
  {
    console.info("长按了长按了");
    var that=this;
    var index = e.currentTarget.dataset.current;
    if (this.data.patientList && index < this.data.patientList.length) {
      var card = this.data.patientList[index];
      if (card) {
        wx.showModal({
          title: '提示',
          content: '确定删除该就诊人吗？',
          confirmColor:"#63ADF7",
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              that.removePatient(card.id);
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }
  },
  add:function(e)
  {
    wx.navigateTo({
      url: '/pages/enter/enter?',
    })
  },
  removePatient:function(id)
  {
    var that = this;
    var param = {};
    param.patientId=id;
    param.userId = app.globalData.shouerUser.userId;

    requestutil.sendRequest("deletePatient", param, function (res) {
      console.info(res);
      wx.stopPullDownRefresh();
      if (res.data.errorCode >= 0) {
        wx.showToast({
          title: "删除就诊人成功!",
          icon: 'none'
        })
        that.getCardList();
      } else {
        wx.showToast({
          title: res.data.errorMsg,
          icon: 'none'
        })
      }

    }, function () {
      console.error("删除就诊人失败");
        wx.stopPullDownRefresh();
        wx.showToast({
          title: "删除就诊人失败!",
          icon: 'none'
        })
    });
  }
})